package com.miyu.client1.utils.bookUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.miyu.client1.dao.book.bookMagicDao;
import com.miyu.client1.utils.ehcache.EhcacheUtil;
import com.miyu.client1.utils.magic.GetBeanUtil;
import com.miyu.client1.utils.redis.redisUtils.util.RedisListUtil;
import com.miyu.client1.utils.redis.redisUtils.util.RedisValUtil;
import org.springframework.beans.factory.annotation.Autowired;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class saveCacheData implements Pipeline {

    RedisListUtil rv = (RedisListUtil) GetBeanUtil.getBeanByClass(RedisListUtil.class);

    @Override
    public void process(ResultItems resultItems, Task task) {
        String key = resultItems.get("key");
        JSONObject vess = new JSONObject();
        switch (resultItems.get("type").toString()){
            case "bookSearch":
                 vess=  resultItems.get("bookSearch");
                EhcacheUtil.getInstance().put("bookEhcache", key, vess);//当前页面写入缓存
                break;
            case "bookChapterList":
                 vess=  resultItems.get("bookChapterList");
                EhcacheUtil.getInstance().put("bookEhcache", key, vess);//当前页面写入缓存
                break;
            case "chapterContext":
                 vess=  resultItems.get("chapterContext");
                EhcacheUtil.getInstance().put("bookEhcache", key, vess);//当前页面写入缓存
                break;
            case "downFile":
                 vess=  resultItems.get("downFile");
                JSONArray jsonArray= JSONArray.parseArray(JSONObject.toJSONString(EhcacheUtil.getInstance().get("bookEhcache","downFile")));
                if(jsonArray==null){
                    JSONArray ja = new JSONArray();
                    ja.add(vess);
                    EhcacheUtil.getInstance().put("bookEhcache", key, ja);//当前页面写入缓存
                }else{
                    jsonArray.add(vess);
                    EhcacheUtil.getInstance().put("bookEhcache", key, jsonArray);//当前页面写入缓存
                }
                break;
            case "initData":
                vess=  resultItems.get("bookChapterList");
                vess.put("type","initData");
                rv.append(1,"initData",vess);
              //  rv.set(1,"initDataChapter",vess);
//            ExecutorService exec = Executors.newFixedThreadPool(1);// 线程数
//            exec.submit(new  InitSaveThead(vess));
                break;
            case "initDataChapter":
                vess=  resultItems.get("chapterContext");
                vess.put("type","initDataChapter");
                vess.put("chapterId",key);
                ExecutorService exec = Executors.newFixedThreadPool(1);// 线程数
                exec.submit(new  InitSaveThead(vess));
                break;
            default:
                break;
        }

    }
}
